package com.yuxinmuyu.sports_management.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuxinmuyu.sports_management.bean.entiy.SportsApply;
import com.yuxinmuyu.sports_management.bean.request.GetSportsApplyRequest;
import com.yuxinmuyu.sports_management.bean.request.GetSportsListRequest;
import com.yuxinmuyu.sports_management.bean.request.InsertSportsApplyRequest;
import com.yuxinmuyu.sports_management.bean.request.*;
import com.yuxinmuyu.sports_management.bean.response.SportsApplyResponse;
import com.yuxinmuyu.sports_management.bean.response.SportsProjectResponse;
import com.yuxinmuyu.sports_management.exception.BusinessException;
import com.yuxinmuyu.sports_management.mapper.SportsApplyMapper;
import com.yuxinmuyu.sports_management.service.SportsApplyService;
import com.yuxinmuyu.sports_management.utils.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SportsApplyServiceImpl implements SportsApplyService {

    @Resource
    private SportsApplyMapper sportsApplyMapper;
    @Override
    public Page<SportsApplyResponse> getSportsApply(GetSportsApplyRequest request) {
        Page<SportsApplyResponse> page = new Page<>(request.getCurrentPage(), request.getPageSize());
        return sportsApplyMapper.getSportsApply(page, request.getYearTermID(), request.getSportsID(),request.getKeyword(),request.getDeptID());
    }

    @Override
    public List<SportsProjectResponse> getselectList(GetSportsListRequest request) {
        return sportsApplyMapper.getSports(request.getYearTermID());
    }

    @Transactional
    @Override
    public void insertSportsApply(InsertSportsApplyRequest request) {
        int n= sportsApplyMapper.insertSportsApply(
                request.getYearTermID(),
                request.getSportsID(),
                UserUtil.getUserId(),
                1,
                new Date(),
                UserUtil.getUserName()
        );
        if (n==0){
            throw new BusinessException("添加失败!");
        }
    }
    public void deleteSportsApply(GetSportsApplyinfoRequest request) {
        int n=sportsApplyMapper.callDeleteSportsApply(request.getApplyID());
        if(n==0){
            throw new BusinessException("删除ID为"+request.getApplyID()+"的报名信息失败!");
        }
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSportsApply(UpdateSportsApplyInfoRequest request) {
        int n=sportsApplyMapper.callUpdate(request.getApplyID(),
                request.getYearTermID(),
                request.getSportsID(),
                request.getUserID(),
                request.getFlag(),
                new Date(),
                UserUtil.getUserName());
        if(n==0){
            throw new BusinessException("删除ID为"+request.getApplyID()+"的项目信息失败!");
    }

    }

    @Override
    public Page<SportsApplyResponse> getSportsApply1(GetSportsApplyRequest request) {
        Page<SportsApplyResponse> page = new Page<>(request.getCurrentPage(), request.getPageSize());
        return sportsApplyMapper.getSportsApply1(page, request.getYearTermID(), request.getSportsID(),request.getKeyword(),request.getDeptID());
    }

    @Override
    public void change(SportsApply request) {
        int n=sportsApplyMapper.update(null, Wrappers.<SportsApply>lambdaUpdate()
                .eq(SportsApply::getApplyID,request.getApplyID())
                .set(SportsApply::getFlag,2));
        if(n==0){
            throw new BusinessException("修改失败！");
        }
    }
}
